﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProjectManageSystemApi.Models;
using ProjectManageSystemApi.Services;
using ProjectManageSystemApi.Utilities;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace ProjectManageSystemApi.Controllers
{
    /// <summary>
    /// 供应商信息Api
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = Grouping.GroupName_v1)]
    public class SupplierController : ControllerBase
    {
        readonly ILogger<AuthController> _logger;
        readonly ISupplierService _spplierService;
        readonly IUserServices _user;
        public SupplierController(ILogger<AuthController> logger, ISupplierService spplierService, IUserServices user)
        {
            _logger = logger;
            _spplierService = spplierService;
            _user = user;
        }
        // GET: api/<SpplierController>
        [HttpPost, Route("GetAll")]
        public ApiResult GetAll([FromBody] PageBo<SupplierParams> pageBo)
        {
            if (pageBo == null)
            {
                pageBo = new PageBo<SupplierParams>();
            }
            if (pageBo.Param == null) 
            {
                pageBo.Param = new SupplierParams();
            }
            var data = _spplierService.GetAll(pageBo);
            return ApiResultStatus.Success(data);
        }

        // POST api/<SpplierController>
        [HttpPost, Route("Create")]
        public ApiResult Create([FromBody] SupplierInfoDto supplierInfo)
        {
            if (supplierInfo == null || string.IsNullOrWhiteSpace(supplierInfo.SupplierName)) 
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            string token = Request.GetToken();
            if (string.IsNullOrWhiteSpace(token))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_user.GetUserIdByToken(token, out long userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            supplierInfo.Id = 0;
            if (_spplierService.ExistBySupplierName(supplierInfo.SupplierName))
            {
                return ApiResultStatus.Fail(ApiResultCodes.SUPPLIER_NAME_EXIST);
            }
            if (!_spplierService.Create(supplierInfo, userId))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        // POST api/<SpplierController>
        [HttpPost, Route("Update")]
        public ApiResult Update([FromBody] SupplierInfoDto supplierInfo)
        {
            if (supplierInfo == null || string.IsNullOrWhiteSpace(supplierInfo.SupplierName) || supplierInfo.Id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (_spplierService.ExistBySupplierName(supplierInfo.SupplierName, supplierInfo.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.SUPPLIER_NAME_EXIST);
            }

            if (!_spplierService.ExistById(supplierInfo.Id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_spplierService.Update(supplierInfo))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }

        // DELETE api/<SpplierController>/5
        [HttpPost, Route("Delete")]
        public ApiResult Delete(int id)
        {
            if (id == 0)
            {
                return ApiResultStatus.Fail(ApiResultCodes.PARAMS_FAILED);
            }
            if (!_spplierService.ExistById(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.DATA_NOT_EXIST);
            }
            if (!_spplierService.Delete(id))
            {
                return ApiResultStatus.Fail(ApiResultCodes.OPERATION_FAILED);
            }
            return ApiResultStatus.Success();
        }
    }
}
